#coding=utf-8
#Boa:FramePanel:PanelPurList

import wx

import DlgPopupSupplier

from product import *
from ctrlAider import *
from MyListCtrl import *

[wxID_PANELPURLIST, wxID_PANELPURLISTBTNALLDATE, wxID_PANELPURLISTBTNONEDAY, 
 wxID_PANELPURLISTBTNONEMONTH, wxID_PANELPURLISTBTNONEWEEK, 
 wxID_PANELPURLISTBTNONEYEAR, wxID_PANELPURLISTBTNSEARCH, 
 wxID_PANELPURLISTBTNSHOWSUPPLIER, wxID_PANELPURLISTDATEEND, 
 wxID_PANELPURLISTDATESTART, wxID_PANELPURLISTTCPEERNAME, 
 wxID_PANELPURLISTTDATE, wxID_PANELPURLISTTPEER, 
] = [wx.NewId() for _init_ctrls in range(13)]

class PanelPurList(wx.Panel):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Panel.__init__(self, id=wxID_PANELPURLIST, name=u'PanelPurList',
              parent=prnt, pos=wx.Point(44, 58), size=wx.Size(965, 681),
              style=wx.TAB_TRAVERSAL)
        self.SetClientSize(wx.Size(957, 647))
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False, u'Arial'))
        self.SetBackgroundColour(wx.Colour(183, 209, 173))

        self.TDate = wx.StaticText(id=wxID_PANELPURLISTTDATE,
              label=u'\u67e5\u8be2\u65e5\u671f\uff1a', name=u'TDate',
              parent=self, pos=wx.Point(22, 38), size=wx.Size(85, 19), style=0)
        self.TDate.SetAutoLayout(True)
        self.TDate.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'Arial'))

        self.dateStart = wx.DatePickerCtrl(id=wxID_PANELPURLISTDATESTART,
              name=u'dateStart', parent=self, pos=wx.Point(126, 30),
              size=wx.Size(120, 27), style=wx.DP_DROPDOWN)
        self.dateStart.SetLabel(u'2014-04-24')
        self.dateStart.SetValue(wx.DateTimeFromDMY(24, 3, 2014, 0, 0, 0))
        self.dateStart.SetToolTipString(u'dateStart')

        self.dateEnd = wx.DatePickerCtrl(id=wxID_PANELPURLISTDATEEND,
              name=u'dateEnd', parent=self, pos=wx.Point(278, 30),
              size=wx.Size(116, 27), style=wx.DP_DROPDOWN)

        self.btnOneDay = wx.Button(id=wxID_PANELPURLISTBTNONEDAY,
              label=u'\u5f53\u65e5', name=u'btnOneDay', parent=self,
              pos=wx.Point(402, 30), size=wx.Size(46, 29), style=0)
        self.btnOneDay.Bind(wx.EVT_BUTTON, self.OnBtnOneDayButton,
              id=wxID_PANELPURLISTBTNONEDAY)

        self.btnOneWeek = wx.Button(id=wxID_PANELPURLISTBTNONEWEEK,
              label=u'\u4e00\u5468', name=u'btnOneWeek', parent=self,
              pos=wx.Point(450, 30), size=wx.Size(46, 29), style=0)
        self.btnOneWeek.Bind(wx.EVT_BUTTON, self.OnBtnOneWeekButton,
              id=wxID_PANELPURLISTBTNONEWEEK)

        self.btnOneMonth = wx.Button(id=wxID_PANELPURLISTBTNONEMONTH,
              label=u'\u4e00\u6708', name=u'btnOneMonth', parent=self,
              pos=wx.Point(498, 30), size=wx.Size(46, 29), style=0)
        self.btnOneMonth.Bind(wx.EVT_BUTTON, self.OnBtnOneMonthButton,
              id=wxID_PANELPURLISTBTNONEMONTH)

        self.btnOneYear = wx.Button(id=wxID_PANELPURLISTBTNONEYEAR,
              label=u'\u4e00\u5e74', name=u'btnOneYear', parent=self,
              pos=wx.Point(546, 30), size=wx.Size(46, 29), style=0)
        self.btnOneYear.Bind(wx.EVT_BUTTON, self.OnBtnOneYearButton,
              id=wxID_PANELPURLISTBTNONEYEAR)

        self.btnAllDate = wx.Button(id=wxID_PANELPURLISTBTNALLDATE,
              label=u'\u5168\u90e8', name=u'btnAllDate', parent=self,
              pos=wx.Point(594, 30), size=wx.Size(46, 29), style=0)
        self.btnAllDate.Bind(wx.EVT_BUTTON, self.OnBtnAllDateButton,
              id=wxID_PANELPURLISTBTNALLDATE)

        self.btnSearch = wx.Button(id=wxID_PANELPURLISTBTNSEARCH,
              label=u'\u67e5\u8be2', name=u'btnSearch', parent=self,
              pos=wx.Point(416, 64), size=wx.Size(80, 29), style=0)
        self.btnSearch.Bind(wx.EVT_BUTTON, self.OnBtnSearchButton,
              id=wxID_PANELPURLISTBTNSEARCH)

        self.btnShowSupplier = wx.Button(id=wxID_PANELPURLISTBTNSHOWSUPPLIER,
              label=u'+', name=u'btnShowSupplier', parent=self,
              pos=wx.Point(376, 64), size=wx.Size(24, 29), style=0)
        self.btnShowSupplier.Bind(wx.EVT_BUTTON, self.OnBtnShowSupplierButton,
              id=wxID_PANELPURLISTBTNSHOWSUPPLIER)

        self.tcPeerName = wx.TextCtrl(id=wxID_PANELPURLISTTCPEERNAME,
              name=u'tcPeerName', parent=self, pos=wx.Point(126, 64),
              size=wx.Size(250, 28), style=0, value=u'')

        self.TPeer = wx.StaticText(id=wxID_PANELPURLISTTPEER,
              label=u'\u4f9b\u8d27\u5546\u540d\u79f0\uff1a', name=u'TPeer',
              parent=self, pos=wx.Point(22, 70), size=wx.Size(102, 19),
              style=0)

    def __init__(self, parent, id):
        self._init_ctrls(parent)
        
        self.myInit()
        
    def myInit(self):
        
        self.dateStart.SetValue(wx.DateTime.Today())
        self.dateEnd.SetValue(wx.DateTime.Today())
        
        self.m_supplier = None
        self.tcPeerName.SetFocus()
        self.popupWin = DlgPopupSupplier.create(self)
        self.tcPeer = MyPopupTextCtrlDecorator(self.tcPeerName, self.popupWin)
        
        self.lcBill = MyListCtrlPurOrder(self)
        self.lcBill.SetSize(wx.Size(950, 200))
        self.lcBill.Move(wx.Point(22,100))
        #self.lcBill.refresh()
        self.lcBill.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnBillListItemSelected)
    
        self.lcContent = MyListCtrlPurContent(self)
        self.lcContent.Move(wx.Point(22,315))
        self.lcContent.SetSize(wx.Size(950, 250))
    
    def setSelectPeer(self, obj): 
        self.m_supplier = obj
        self.tcPeer.ChangeValue(obj.getValue(obj.COL_NAME))
            
    def OnBillListItemSelected(self, event):
        '''按照选中订单，显示订单内容'''
        item = self.lcBill.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        #orderId = self.orderList.GetItem(item, self.orderList.COL_ID).GetText()
        billId = self.lcBill.getBillId(item)
        self.lcContent.refresh({'billId':billId})
        
    def OnBtnSearchButton(self, event):
        '''按照搜索条件，显示订单信息'''
        filter = {'dateStart':self.dateStart.GetValue().Format('%Y-%m-%d'),
                    'dateEnd':self.dateEnd.GetValue().Format('%Y-%m-%d')}

        if self.tcPeer.GetValue() == '':
            #self.tcPeer.ChangeValue(u'全部客户')
            self.m_supplier = None
                
        if self.m_supplier is not None:
            filter['peerId'] = self.m_supplier.getValue(CDataSupplier.COL_ID)
            #filter['custName'] = self.customer.getFieldVal(field='name')

        cnt = self.lcBill.refresh(filter)
        if (cnt == 0):
            dlg = wx.MessageDialog(self, u'没有符合条件的记录', u'提示消息', wx.OK|wx.ICON_INFORMATION)
            dlg.ShowModal()

    def OnBtnShowSupplierButton(self, event):
        self.tcPeer.resetPopupWinPos()
        self.popupWin.Show() 
                
    def OnBtnOneDayButton(self, event):
        self.dateStart.SetValue(wx.DateTime.Today())
        self.dateEnd.SetValue(wx.DateTime.Today())

    def OnBtnOneWeekButton(self, event):
        day = wx.DateTime.Today()
        self.dateEnd.SetValue(day)
        
        span = wx.DateSpan.Days(7)
        day.SubtractDS(span)
        self.dateStart.SetValue(day)
        
    def OnBtnOneMonthButton(self, event):
        day = wx.DateTime.Today()
        self.dateEnd.SetValue(day)
        
        span = wx.DateSpan.Months(1)
        day.SubtractDS(span)
        self.dateStart.SetValue(day)

    def OnBtnOneYearButton(self, event):
        day = wx.DateTime.Today()
        self.dateEnd.SetValue(day)
        
        span = wx.DateSpan.Years(1)
        day.SubtractDS(span)
        self.dateStart.SetValue(day)

    def OnBtnAllDateButton(self, event):
        self.dateEnd.SetValue(wx.DateTime.Today())        
        self.dateStart.SetValue(wx.DateTimeFromDMY(1,0,2000,0,0,00))

